package com.jusfoun.communication.controller;

import com.jusfoun.communication.model.User;
import com.jusfoun.communication.service.ManageService;
import com.jusfoun.communication.service.UserService;
import com.jusfoun.communication.util.PageInfo;
import com.jusfoun.communication.util.Result;
import com.jusfoun.communication.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 用户管理接口
 *
 * @author player six
 * @email jslcylcy@163.com
 * @create 2017-12-06 15:23
 **/
@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(description = "用户管理接口")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private ResultUtil resultUtil;

    @Autowired
    private ManageService manageService;

    @Autowired
    private UserService userService;


    /**
     * 传入appId  appSecret userName 生成appToken(一周有效,重新调用之前的token会被刷新)
     *
     * @return
     */
    @RequestMapping(value = "/generate-token", method = RequestMethod.POST)
    @ApiOperation(value = "获取appToken", notes = "根据传入的appId,appSecret,userName生成appToken,一周有效，刷新会使之前的token失效")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "form", dataType = "String"),
                    @ApiImplicitParam(name = "appSecret", value = "appSecret", required = true, paramType = "form", dataType = "String"),
                    @ApiImplicitParam(name = "userName", value = "用户名称", required = true, paramType = "form", dataType = "String"),
            }
    )
    public Result generateToken(@ApiIgnore User user) {
        logger.info("获取appId:" + user.getAppId() + ",userName:" + user.getUserName() + ",token");
        if (StringUtils.isBlank(user.getAppId()) || StringUtils.isBlank(user.getAppSecret()) || StringUtils.isBlank(user.getUserName())) {
            logger.info("缺少参数");
            return resultUtil.fail("失败", "数据格式错误");
        }
        return manageService.generateToken(user);
    }

    /**
     * 给用户设定分组,一个用户只能属于一个分组
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/user-group", method = RequestMethod.POST)
    @ApiOperation(value = "设置用户分组", notes = "设置用户所属分组，每个用户只能属于一个分组")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "form", dataType = "String"),
                    @ApiImplicitParam(name = "appSecret", value = "appSecret", required = true, paramType = "form", dataType = "String"),
                    @ApiImplicitParam(name = "userName", value = "用户名称", required = true, paramType = "form", dataType = "String"),
                    @ApiImplicitParam(name = "group", value = "用户分组", required = true, paramType = "form", dataType = "String"),
            }
    )
    public Result userGroup(@ApiIgnore User user) {
        logger.info("设定用户分组appId:" + user.getAppId() + ",userName:" + user.getUserName() + ",group:" + user.getGroup());
        if (StringUtils.isBlank(user.getAppId()) || StringUtils.isBlank(user.getAppSecret()) || StringUtils.isBlank(user.getUserName())) {
            logger.info("缺少参数");
            return resultUtil.fail("失败", "缺少参数");
        }
        if (!manageService.checkAppIdAndSecret(user.getAppId(), user.getAppSecret())) {
            logger.info("appId与appSecret不匹配");
            return resultUtil.fail("失败", "appId与appSecret不一致");
        }
        return userService.userGroup(user);
    }

    /**
     * 给用户设定标签,一个用户可以属于多个标签,覆盖之前设置的标签,标签以,分隔
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/user-tags", method = RequestMethod.POST)
    @ApiOperation(value = "设置用户标签", notes = "设置用户标签，每个用户可以有多个标签")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "form", dataType = "String"),
                    @ApiImplicitParam(name = "appSecret", value = "appSecret", required = true, paramType = "form", dataType = "String"),
                    @ApiImplicitParam(name = "userName", value = "用户名称", required = true, paramType = "form", dataType = "String"),
                    @ApiImplicitParam(name = "tags", value = "用户标签以,分隔", required = true, paramType = "form", dataType = "String"),
            }
    )
    public Result userTags(@ApiIgnore User user) {
        logger.info("设定用户tags,appId:" + user.getAppId() + ",userName:" + user.getUserName() + ",tags:" + user.getTags());
        if (StringUtils.isBlank(user.getAppId()) || StringUtils.isBlank(user.getAppSecret()) || StringUtils.isBlank(user.getUserName())) {
            logger.info("缺少参数");
            return resultUtil.fail("失败", "缺少参数");
        }
        if (!manageService.checkAppIdAndSecret(user.getAppId(), user.getAppSecret())) {
            logger.info("appId与appSecret不匹配");
            return resultUtil.fail("失败", "appId与appSecret不一致");
        }
        return userService.userTags(user);
    }

    /**
     * 获取tag下所有用户,可分页
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/tag-user", method = RequestMethod.GET)
    @ApiOperation(value = "获取标签下用户", notes = "获取指定标签下用户")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "appSecret", value = "appSecret", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "tag", value = "标签", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "pageNum", value = "页码", required = false, dataType = "int", paramType = "query"),
                    @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, dataType = "int", paramType = "query"),
            }
    )
    public PageInfo<String> getTagUsers(String tag, @ApiIgnore User user, Integer pageNum, Integer pageSize) {
        logger.info("获取tag下用户列表,appId:" + user.getAppId() + ",tag:" + tag);
        if (StringUtils.isBlank(user.getAppId()) || StringUtils.isBlank(user.getAppSecret())) {
            logger.info("appId与appSecret为空");
            return new PageInfo<>();
        }
        if (!manageService.checkAppIdAndSecret(user.getAppId(), user.getAppSecret())) {
            logger.info("appId与appSecret不匹配");
            return new PageInfo<>();
        }
        return userService.getTagUsers(tag, user.getAppId(), pageNum, pageSize);
    }

    /**
     * 获取group下所有用户,可分页
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/group-user", method = RequestMethod.GET)
    @ApiOperation(value = "获取分组下用户", notes = "获取指定分组下用户")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "appSecret", value = "appSecret", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "group", value = "分组", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "pageNum", value = "页码", required = false, dataType = "int", paramType = "query"),
                    @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, dataType = "int", paramType = "query"),
                    @ApiImplicitParam(name = "status", value = "用户状态,在线或者离线", required = false, dataType = "String", paramType = "query", allowableValues = "online,offline"),
            }
    )
    public PageInfo<String> getGroupUsers(@ApiIgnore User user, Integer pageNum, Integer pageSize) {
        logger.info("获取group下用户列表,appId:" + user.getAppId() + ",group:" + user.getGroup());
        if (StringUtils.isBlank(user.getAppId()) || StringUtils.isBlank(user.getAppSecret())) {
            logger.info("appId与appSecret为空");
            return new PageInfo<>();
        }
        if (!manageService.checkAppIdAndSecret(user.getAppId(), user.getAppSecret())) {
            logger.info("appId与appSecret不匹配");
            return new PageInfo<>();
        }
        return userService.getGroupUsers(user, pageNum, pageSize);
    }

    /**
     * 获取所有分组,可分页
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/groups", method = RequestMethod.GET)
    @ApiOperation(value = "获取app下所有分组列表", notes = "获取app下所有分组列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "appSecret", value = "appSecret", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "pageNum", value = "页码", required = false, dataType = "int", paramType = "query"),
                    @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, dataType = "int", paramType = "query"),
            }
    )
    public PageInfo<String> getGroup(@ApiIgnore User user, Integer pageNum, Integer pageSize) {
        logger.info("获取app下所有group,appId:" + user.getAppId());
        if (StringUtils.isBlank(user.getAppId()) || StringUtils.isBlank(user.getAppSecret())) {
            logger.info("appId与appSecret为空");
            return new PageInfo<>();
        }
        if (!manageService.checkAppIdAndSecret(user.getAppId(), user.getAppSecret())) {
            logger.info("appId与appSecret不匹配");
            return new PageInfo<>();
        }
        return userService.getGroups(user.getAppId(), pageNum, pageSize);
    }

    /**
     * 获取所有标签,可分页
     *
     * @param user
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/tags", method = RequestMethod.GET)
    @ApiOperation(value = "获取app下所有标签列表", notes = "获取app下所有标签列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "appSecret", value = "appSecret", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "pageNum", value = "页码", required = false, dataType = "int", paramType = "query"),
                    @ApiImplicitParam(name = "pageSize", value = "每页数量", required = false, dataType = "int", paramType = "query"),
            }
    )
    public PageInfo<String> getTags(@ApiIgnore User user, Integer pageNum, Integer pageSize) {
        logger.info("获取app下所有tag,appId:" + user.getAppId());
        if (StringUtils.isBlank(user.getAppId()) || StringUtils.isBlank(user.getAppSecret())) {
            logger.info("appId与appSecret为空");
            return new PageInfo<>();
        }
        if (!manageService.checkAppIdAndSecret(user.getAppId(), user.getAppSecret())) {
            logger.info("appId与appSecret不匹配");
            return new PageInfo<>();
        }
        return userService.getTags(user.getAppId(), pageNum, pageSize);
    }

    /**
     * 获取用户状态(是否在线)
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/user-status", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户状态", notes = "获取用户状态(是否在线)")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "appId", value = "appId", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "appSecret", value = "appSecret", required = true, paramType = "query", dataType = "String"),
                    @ApiImplicitParam(name = "userName", value = "用户名称", required = true, dataType = "String", paramType = "query"),
            }
    )
    public Result getUserStatus(@ApiIgnore User user) {
        logger.info("获取用户状态:" + user.getAppId() + ",userName:" + user.getUserName());
        if (StringUtils.isBlank(user.getAppId()) || StringUtils.isBlank(user.getAppSecret())) {
            logger.info("appId与appSecret为空");
            return resultUtil.fail("失败", "缺少参数");
        }
        if (!manageService.checkAppIdAndSecret(user.getAppId(), user.getAppSecret())) {
            logger.info("appId与appSecret不匹配");
            return resultUtil.fail("失败", "appId与appSecret不匹配");
        }
        return userService.checkUserStatus(user.getAppId(), user.getUserName());
    }
}
